<template>
  <view class="auth p-10px">
    <u-search v-model="keyword" @change="handlerKeywordChanage"/>
    <u-list class="flex-1">
      <template v-for="item in schoolList">
        <u-list-item>
          <view @click="handlerSchoolItemClick(item)">
            {{item.name}}
          </view>
        </u-list-item>
      </template>
    </u-list>
    <view class="flex">
      <u-button class="flex-1 mr-10px">确认</u-button>
      <u-button class="flex-1">确认</u-button>
    </view>
  </view>
</template>

<script lang="ts" setup>

import type {School} from "@/api/book";
import {ref} from "vue";
import {onLoad} from "@dcloudio/uni-app";
import USearch from "@/uni_modules/uview-plus/components/u-search/u-search.vue";
import UList from "@/uni_modules/uview-plus/components/u-list/u-list.vue";
import UListItem from "@/uni_modules/uview-plus/components/u-list-item/u-list-item.vue";
import UImage from "@/uni_modules/uview-plus/components/u--image/u--image.vue";
import UText from "@/uni_modules/uview-plus/components/u-text/u-text.vue";
import UButton from "@/uni_modules/uview-plus/components/u-button/u-button.vue";

// 搜索框关键字
const keyword = ref('')

const schoolList = ref<School[]>([])
// 获取学校列表
const fnGetSchoolList = () => {
  const params = {
    scroll_name: keyword.value
  }
  schoolList.value = [
    {
      sId: 1,
      name: 'string',
      type: 0,
      area: 'string',
      createTime: '2023-01-01',
      updateTime: '2023-01-01'
    }
  ]
}
// 初始化学校列表
fnGetSchoolList()
// 搜索框关键字输入事件
const handlerKeywordChanage = uni.$u.debounce((value: string) => {
  // 搜索学校
  fnGetSchoolList()
},200)
const value = ref<School | null>(null)
const fnCallbackValue = () => {
  let pages = getCurrentPages() // 获取当前页面栈的实例，以数组形式按栈的顺序给出，第一个元素为首页，最后一个元素为当前页面。
  let prevPage = pages[pages.length - 2] //上一页页面实例
  uni.navigateBack({
    delta: 1,
    success(){
      console.log('prevPage', prevPage)
      prevPage.$vm.fnSelectSchool(value.value)
    }
  })
}
// 学校列表子项点击事件
const handlerSchoolItemClick = (item:School) => {
  value.value = item
  fnCallbackValue()
}
</script>

<style scoped lang="scss">
.toast-box{
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
  width: 80%
}
</style>